package com.cz.video.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.video.common.constant.CommonConstants;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.exception.ServiceException;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.website.domain.WebsiteCollect;
import com.cz.video.website.domain.WebsiteCollectAndFolder;
import com.cz.video.website.domain.WebsiteCollectFolder;
import com.cz.video.website.domain.bo.WebsiteCollectBo;
import com.cz.video.website.domain.vo.WebsiteCollectVo;
import com.cz.video.website.mapper.WebsiteCollectAndFolderMapper;
import com.cz.video.website.mapper.WebsiteCollectFolderMapper;
import com.cz.video.website.mapper.WebsiteCollectMapper;
import com.cz.video.website.service.IWebsiteCollectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author yr
 * @since 2023-10-15
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class WebsiteCollectServiceImpl extends ServiceImpl<WebsiteCollectMapper, WebsiteCollect> implements IWebsiteCollectService {
    private final WebsiteCollectMapper websiteCollectMapper;
    private final WebsiteCollectAndFolderMapper websiteCollectAndFolderMapper;
    private final WebsiteCollectFolderMapper collectFolderMapper;

    @Override
    public int insertCollect(WebsiteCollectBo websiteCollect) {
        checkFolder(websiteCollect);
        // 新增收藏表信息
        int rows =  websiteCollectMapper.insert(websiteCollect);
        return rows;
    }

    private void checkFolder(WebsiteCollectBo websiteCollect) {
        //检查收藏夹是否是这个用户的
        if (websiteCollect.getCollectFolderId()!=null){
            WebsiteCollectFolder folder = collectFolderMapper.selectById(websiteCollect.getCollectFolderId());
            if (folder == null) {
                throw new ServiceException("收藏夹不存在");
            }
            //如果不是同一个用户的就抛异常
            if (!folder.getUserId().equals(LoginHelper.getUserId())) {
                throw new ServiceException("不能添加到别人的收藏夹中");
            }
        }
    }

    public void insertCollectAndFolder(WebsiteCollectBo websiteCollect) {
        this.insertCollectAndFolder(websiteCollect.getCollectId(), websiteCollect.getCollectFolderId());
    }
    /**
     * 新增收藏和收藏夹信息
     */
    public void insertCollectAndFolder(Long c_id,Long f_id)
    {
        WebsiteCollectAndFolder cf = new WebsiteCollectAndFolder();
        cf.setCollectId(c_id);
        cf.setCollectFolderId(f_id);
        websiteCollectAndFolderMapper.insert(cf);
    }

    /**
     * 根据收藏id删除
     * @param id
     * @return
     */
    @Override
    public int deleteById(Long[] id) {
        return websiteCollectMapper.delete(Wrappers.<WebsiteCollect> lambdaQuery()
                .eq(WebsiteCollect::getUserId, LoginHelper.getUserId())
            .in(WebsiteCollect::getCollectId, (Object[]) id));
    }

    /**
     * 修改收藏
     * @param websiteCollect
     * @return
     */
    @Override
    public int updateCollect(WebsiteCollectBo websiteCollect) {
        checkFolder(websiteCollect);
        return websiteCollectMapper.updateById(websiteCollect);
    }
    /**
     * 分页查询
     * @param websiteCollect
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<WebsiteCollectVo> selectPageCollectList(WebsiteCollectBo websiteCollect, PageQuery pageQuery) {
        Page<WebsiteCollectVo> page = websiteCollectMapper.selectPageCollectList(pageQuery.build(), this.buildQueryWrapper(websiteCollect));
        return TableDataInfo.build(page);
    }

    @Override
    public int delete(Long collectFolderId, Long videoId) {
        return websiteCollectMapper.delete(Wrappers.<WebsiteCollect>lambdaQuery()
            .eq(WebsiteCollect::getTargetId, videoId)
            .eq(WebsiteCollect::getUserId,LoginHelper.getUserId())
            .eq(WebsiteCollect::getCollectFolderId, collectFolderId));
    }

    private Wrapper<WebsiteCollect> buildQueryWrapper(WebsiteCollectBo websiteCollect) {
        Map<String, Object> params = websiteCollect.getParams();
        QueryWrapper<WebsiteCollect> wrapper = Wrappers.query();

        wrapper.eq("wc.del_flag", CommonConstants.NOT_DELETED)
            .eq(ObjectUtil.isNotNull(websiteCollect.getCollectFolderId())&&websiteCollect.getCollectFolderId()!=0, "wc.collect_folder_id", websiteCollect.getCollectFolderId())
            .isNull(ObjectUtil.isNotNull(websiteCollect.getCollectFolderId())&&websiteCollect.getCollectFolderId()==0, "wc.collect_folder_id")
            .eq(ObjectUtil.isNotNull(websiteCollect.getUserId()), "wc.user_id", websiteCollect.getUserId())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                "wc.create_time", params.get("beginTime"), params.get("endTime"));
        return wrapper;
    }

}


